import {_decorator, Node} from 'cc';

import {ItemStatusEnum, ItemTypeEnum, TriggerStatusEnum, TriggerTypeEnum} from "db://assets/Scripts/Enum";
import {TriggerManager} from './TriggerManager';
import DataManager from "db://assets/Scripts/Runtime/DataManager";

const {ccclass, property} = _decorator;


@ccclass('MailboxTriggerManager')
export class MailboxTriggerManager extends TriggerManager {
    type: TriggerTypeEnum = TriggerTypeEnum.MailBox

    @property(Node)
    closeNode: Node = null

    @property(Node)
    openNode: Node = null

    render() {
        super.render()
        const open = DataManager.Instance.mailboxStatus === TriggerStatusEnum.Resolved
        this.closeNode.active = !open
        this.openNode.active = open
    }

    handleTrigger() {
        const selectedKey = DataManager.Instance.curItemType === ItemTypeEnum.Key && DataManager.Instance.isSelected // 当前背包选中了钥匙
        if (selectedKey) { // 钥匙的使用逻辑
            DataManager.Instance.curItemType = null
            DataManager.Instance.isSelected = false
            // 移除钥匙
            DataManager.Instance.items.find(i => i.type === ItemTypeEnum.Key).status = ItemStatusEnum.Disabled
            // 设置 Mail 到场景中
            DataManager.Instance.items.find(i => i.type === ItemTypeEnum.Mail).status = ItemStatusEnum.Scene
            DataManager.Instance.items = [...DataManager.Instance.items]
            // 设置邮箱被打开状态
            DataManager.Instance.mailboxStatus = TriggerStatusEnum.Resolved
        }
    }
}